local rule = fk.CreateSkill {
  name = "#hxstar_shop_rule&",
}

local HX = require "packages/huixiangkill/hxfs"

--结算金豆
rule:addEffect(fk.GameFinished, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    if player ~= nil then
      return player.role == "lord"
    end
    return false
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local lord = room:getLord() or player
    local point = room:getBanner("@hx__1v2_mode_POINT") or 100
    local players = table.filter(room.players, function (p) return p.id > 0 end)
    local others = table.filter(players, function (p) return p.role ~= "lord" end)
    local win = data:split("+")
    local lost = math.ceil(point / 2)
    if table.contains(win, lord.role) then
      for _, p in ipairs(others) do
        HX.ChangePlayerMoney(room, "hx_1v2_jindou", p, -lost)
      end
      HX.ChangePlayerMoney(room, "hx_1v2_jindou", player, point)
    else
      HX.ChangePlayerMoney(room, "hx_1v2_jindou", player, -point)
      for _, p in ipairs(others) do
        HX.ChangePlayerMoney(room, "hx_1v2_jindou", p, lost)
      end
    end
    for _, p in ipairs(players) do
      local money = HX.GetPlayerMoney(room, "hx_1v2_jindou", p)
      if money < 0 then
        p:doNotify("ShowToast", "当前金豆数："..money.."，你已破产！金豆恢复至正数前无法抢地主！")
        HX.ChangePlayerMoney(room, "hx_1v2_jindou", p, 100)
      end
    end
  end,
})


return rule
